package com.zhao.controller;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.zhao.service.ConsumerBasicInfoService;
import com.zhao.utils.MyRunnable1;
import com.zhao.utils.RedisUtil;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 描述:用户信息控制层
 *
 * @author Zhao
 * @create 2021-05-20 1:43
 */
@Slf4j
@RestController
@RequestMapping("/user")
@RefreshScope
public class UserController {

    private final ConsumerBasicInfoService consumerBasicInfoService;
    private final RedisUtil redisUtil;
    private ThreadPoolTaskScheduler threadPoolTaskScheduler;

    private Map< String ,ScheduledFuture<?>> futureMap = new HashMap<>();
    private ScheduledFuture<?> future;

    public UserController(ConsumerBasicInfoService consumerBasicInfoService, RedisUtil redisUtil,ThreadPoolTaskScheduler threadPoolTaskScheduler) {
        this.consumerBasicInfoService = consumerBasicInfoService;
        this.redisUtil = redisUtil;
        this.threadPoolTaskScheduler = threadPoolTaskScheduler;
    }

    /**
     * description 数据持久化测试接口 <br/>
     * version 1.0
     * @date 21.8.12 2:43
     * @author Zhao
     * @param
     * @return java.lang.String
     */
    @RequestMapping("/getUserInfoList")
    public String getUserInfoList() throws InterruptedException {
        Snowflake snowflake = IdUtil.getSnowflake(1, 1);
        redisUtil.set("token",snowflake.nextIdStr());
        redisUtil.expire("token", 10, TimeUnit.SECONDS);
        log.info(redisUtil.get("token"));
        return JSON.toJSONString(consumerBasicInfoService.list());
    }


    /**
     * description: 开启定时任务接口 <br/>
     * @version 1.0
     * @date: 21.8.12 2:45
     * @author zhangzhao
     * @param dispatchId 任务id
     * @param time 任务周期时间
     * @return java.lang.String
     */
    @RequestMapping("/startDispatch")
    public String startDispatch(String dispatchId,String time){
        ScheduledFuture<?> scheduledFuture = this.futureMap.get(dispatchId);
        if (scheduledFuture != null){
            return "定时任务已启动！";
        }else {
            future = threadPoolTaskScheduler.schedule(new MyRunnable1(dispatchId,time), new Trigger() {
                @Override
                public Date nextExecutionTime(TriggerContext triggerContext) {
                    return new CronTrigger("0/"+time+" * * * * *").nextExecutionTime(triggerContext);
                }
            });
            futureMap.put(dispatchId, future);
            System.out.println("定时任务正在执行-----");
            return dispatchId+"===>定时任务启动成功！任务周期为"+time+"s";
        }
    }
    /**
     * description: 结束任务接口 <br/>
     * @version 1.0
     * @date: 21.8.12 2:46
     * @author zhangzhao
     * @param dispatchId 任务id
     * @return java.lang.String
     */
    @RequestMapping("/stopDispatch")
    public String stopDispatch(String dispatchId){
        if (futureMap.size()>0) {
            ScheduledFuture<?> future = futureMap.get(dispatchId);
            if (future!=null) {
                future.cancel(true);
                System.out.println("定时任务关闭");
                return dispatchId+"定时任务关闭成功!";
            }
        }
        return dispatchId+"===>定时任务不存在！";
    }
}
